package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.Comment;
import com.vne.skyemart.service.CommentService;

@Controller
@SessionAttributes
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	private CommentService commentService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveComment(@ModelAttribute(" comment") Comment comment,
			BindingResult result) {
		 commentService.addComment(comment);
		return new ModelAndView("redirect:/comment.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listComment(Model model) {
		model.addAttribute("comment",  commentService.listComment());

		return "commentList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addComment(@ModelAttribute("comment") Comment comment,
			BindingResult result) {
		return new ModelAndView("addComment");
	}
}